﻿using OhioState.Graphics;
using OhioState.Graphics.OpenGL;
using OhioState.Graphics.Proxy;
using OhioState.AssetManaged;

namespace UI_Controls
{
    internal class RenderServer
    {
        public IRenderPanel Panel
        {
            get { return panel; }
        }

        public RenderServer(ViewProxy view, IRenderPanel panel)
        {
            if (panel != null)
            {
                this.panel = panel;
            }
            else
            {
                CreateNewForm();
            }
            CreateViewHandler(view);
            view.AddChangedEventHandler(Invalidate);
        }

        public void Invalidate(IAssetManaged asset)
        {
            panel.Invalidate();
        }

        private void CreateNewForm()
        {
            OpenGLForm newWindow = new OpenGLForm();
            this.panel = newWindow.RenderPanel;
            newWindow.Show();
        }

        private void CreateViewHandler(ViewProxy view)
        {
            //
            // Hook the view's composite layers to the render event.
            // This should be changed at some point to provide a clean separation of OpenGL-based objects
            // from the Shared logic, as well as allow for multiple views and multiple glPanels.
            //
            viewHandler = new OhioState.Graphics.OpenGL.ViewHandler(view);
            panel.renderScene += new RenderScene(viewHandler.CompositeLayers);
        }

        #region Member Variables
        private ViewHandler viewHandler;
        private IRenderPanel panel;
        #endregion
    }
}
